
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include <Inventor/nodes/SoSeparator.h>

#include "scenegraph.hpp"
#include "simulationwin.hpp"

#include "cutter.hpp"
#include "point.hpp"

#include "position_solutions.h"
#include "notifiers.hpp"

#include <iostream>
#include <string>
#include <iterator>
#include <QProgressBar>
#include <QCoreApplication>

using namespace std;

extern "C"
{
  void valgrind_start_mark()
  {
  }
   void valgrind_stop_mark()
  {
  }
}



simulationwin::simulationwin(QWidget *parent):QWidget(parent)
{
    setupUi(this);

    root=new SoSeparator;
    root->ref();

    viewer=new SoQtExaminerViewer(this->viewplace);
    viewer->setDecoration(false);
    viewer->setSceneGraph(root);


}

class Note: public facet_note
{
public:
    Note(QProgressBar* _pb)
    {
        pb=_pb;
    }
    void tic(string s, int v)
    {
        pb->setValue(v);
        QCoreApplication::processEvents();
    }

    QProgressBar *pb;
};

void simulationwin::prepare()
{
    cerr<<endl<<model::xmin<<" "<<model::xmax<<"\n"
    <<model::ymin<<" "<<model::ymax<<"\n"
    <<model::zmin<<" "<<model::zmax<<"\n";
     blank.blank(model::xmin,
                model::xmax,
		 model::ymin,
                 model::ymax,
                 model::zmin,
                 model::zmax+5,
                0.5,0.5);
    //  blank.blank(-5.0,55.0,
//                -55.0,5.0,
//                 -5.0,30.0,
//                 0.5,0.5);
    
    

    Note note(progress);
    
    blank.facet.facet(&note);
    
    root->addChild(blank.facet.Result());
   
    viewer->viewAll();

   cylinder_cutter cut(model::tooldiam,30,0);
    root->addChild(cut.getview());

    double xmin, xmax,
    ymin,ymax,
    zmin,zmax;

    xmin=ymin=zmin=1000;
    xmax=ymax=zmax=-1000;

    long uptimes=1;
    if(model::path.size()>40)
     uptimes=model::path.size()/4;
    if(model::path.size()>400)
     uptimes=model::path.size()/10;
   
    //
    //
    // vector<vertical_cylinder> cuts;


    valgrind_start_mark();
    
    for (long i=0;
            i<model::path.size();
            i++)
    {
      //cuts.push_back(vertical_cylinder(model::tooldiam,30,0));
         point &p=model::path[i];
         cut.set_orient(build_trans(p));
//        root->addChild(cuts.at(i).getview());
//        QCoreApplication::processEvents();
//       }
    
        xmin=min(xmin,cut.xmin());
        ymin=min(ymin,cut.ymin());
        zmin=min(zmin,cut.zmin());

        xmax=max(xmax,cut.xmax());
        ymax=max(ymax,cut.ymax());
        zmax=max(zmax,cut.zmax());
	

        blank.apply_cutter(&cut);
	//	if(i%5==0)
	  note.tic("",i*100/model::path.size());
	if ((i+1)%uptimes==0)
	  {
	    blank.facet.facet(&note);
	      
	  }
	


    }
 
     valgrind_stop_mark();
}

void simulationwin::on_RefreshButton_clicked(){
blank.facet.facet();
cerr<<"you!!!!\n";
}

void simulationwin::on_SwitchModeButton_toggled( bool checked ){
	if(checked)
		viewer->setDrawStyle(SoQtViewer::STILL,SoQtViewer::VIEW_BBOX);
	else
		viewer->setDrawStyle(SoQtViewer::STILL,SoQtViewer::VIEW_AS_IS);
}

simulationwin::~simulationwin()
{
    delete viewer;
    root->unref();
}

